<?php

class UrlManager
{
    protected $_manager = NULL;
    protected $_blog    = NULL;

    /**
     *  init
     */
    public function __construct( $manger=null )
    {
        if( $manger ) {
            $this->setManager( $manger );
        }
    }

    /**
     *  set create url manger object
     */
    public function setManager( $manger )
    {
        $this->_manager = $manger;
    }

    /**
     *  如果要使用 blog 產生網址相關的 method, 就必須要設定該值
     *  set blog
     */
    public function setBlog( $blog )
    {
        $this->_blog = $blog;
    }

    /**
     *  get base path
     *  取得基本路徑
     *
     *      CONST SYSTEM_PATH_TYPE   = 1;  // 系統絕對位置      => /var/www/project
     *      CONST WEB_PATH_TYPE      = 2;  // 網路相對位置      => /project
     *      CONST HTTP_PATH_TYPE     = 3;  // 網頁位置          => http://localhost/project
     *      CONST HTTPS_PATH_TYPE    = 4;  // 網頁位置          => https://localhost/project
     *      CONST YII_VIEW_PATH_TYPE = 5;  // YII view 相對位置 => //project
     *
     */
    public function getBasePath( $pathType=null )
    {
        if( !$pathType ) {
            $pathType = Controller::WEB_PATH_TYPE;
        }

        switch($pathType) {
            case Controller::SYSTEM_PATH_TYPE:
                $basePath = Request::getDocumentRoot() . Yii::app()->request->baseUrl;
                break;
            case Controller::HTTP_PATH_TYPE:
                $basePath = 'http://'. Request::getHost() . Yii::app()->request->baseUrl;
                break;
            case Controller::HTTPS_PATH_TYPE:
                $basePath = 'https://'. Request::getHost() . Yii::app()->request->baseUrl;
                break;
            case Controller::YII_VIEW_PATH_TYPE:
                $basePath = '//../..';
                break;
            case Controller::WEB_PATH_TYPE:
                $basePath = Yii::app()->request->baseUrl;
                break;
            default:
                $basePath = '';
                break;
        }

        return $basePath;
    }

    // ====================================================================================================

    /**
     *  基本產生 網址 功能
     */
    public function get( $controllerName, $actionName, $model=null )
    {
        $action = $controllerName.'/'.$actionName;

        switch( strtolower($action) ) {
            case 'adminsiteclient/editdependant':
                return $this->_manager->createUrl( $action , array('parentId'=>$model->getClientId(), 'id'=>$model->getId()) );
                break;
            default:
                // 基本規格猜測法
                if( !$model ) {
                    return $this->_manager->createUrl( $action );
                }
                else {
                    return $this->_manager->createUrl( $action , array('id'=>$model->getId()) );
                }
                break;
        }

    }

    /**
     *  基本產生 列表、分頁列表、分頁搜尋列表 功能
     */
    public function getList( $controllerName, $model, $page=1, $searchKey='' )
    {
        $action = $controllerName.'/list';
        if($searchKey) {
            return $this->_manager->createUrl( $action, Array('page'=>$page,'searchKey'=>$searchKey) );
        }
        elseif($page>1) {
            return $this->_manager->createUrl( $action, Array('page'=>$page) );
        }
        else {
            return $this->_manager->createUrl( $action );
        }
    }

    /* ================================================================================

        產生 blog 網址:

            index.php/blog/1
            index.php/blog/1/home
            index.php/blog/1/page/5
            index.php/blog/1/word/天氣好            => comment list or comment post => article/10
            index.php/blog/1/article/10             => comment list or comment post
            index.php/blog/1/article/10/page/5      => 同上
            index.php/blog/1/article/10/comment/20  => reply comment post
            index.php/blog/1/search
            index.php/blog/1/search/%20%20%20
            index.php/blog/1/search/%20%20%20/page/5
            index.php/blog/1/category/30
            index.php/blog/1/category/30/page/5
            index.php/blog/1/rss/2.0/blog
            index.php/blog/1/rss/2.0/category/30
            index.php/blog/1/resource/40
            index.php/blog/1/resource/40/view
            index.php/blog/1/resource/40/download

    ================================================================================ */

    // blog home
    public function blogHome( $page=null )
    {
        $url = 'blog/'.$this->_blog->getId();
        if($page) {
            $url .= '/page/'.$page;
        }
        return $this->_manager->createUrl( $url );
    }

    // blog article
    public function blogArticle( $article, $page=null )
    {
        $url = 'blog/'.$this->_blog->getId();
        if(is_object($article)) {
            $url .= '/article/'. $article->getId();
        }
        else {
            $url .= '/article/'. $article;
        }

        if($page) {
            $url .= '/page/'.$page;
        }
        return $this->_manager->createUrl( $url );
    }

    // blog article comment
    public function blogComment( $comment, $article )
    {
        $url = 'blog/'.$this->_blog->getId();
        if(is_object($comment)) {
            $url .= '/article/'. $comment->getArticleId(). '/'. $comment->getId();
        }
        elseif(is_object($article)) {
            $url .= '/article/'. $comment. '/'. $article->getId();
        }
        else {
            $url .= "/article/{$article}/comment/{$comment}";
        }
        return $this->_manager->createUrl( $url );
    }

    // blog rss
    public function blogRss( $version="2.0", $type='blog', $categoryId=null )
    {
        if( 'category'==$type && $categoryId ) {
            $url = "blog/rss/{version}/category/{$categoryId}";
        }
        else {
            $url = "blog/rss/{version}/blog";
        }
        return $this->_manager->createUrl( $url );
    }




    public function blog( $actionName='home', $model='' )
    {


        $action = 'blog/'. $model->getId();

        if( 'home' == $actionName ) {
            //$action .= '/'. $actionName;
            return $this->_manager->createUrl( $action );
        }

        $blogId = $model->getBlogId();
        $action = 'blog/'. $blogId. '/'. $actionName;

        $class = get_class($model);
        if( !$class ) {
            return $this->_manager->createUrl( $action );
        }

        switch( $class ) {
            case 'BlogArticle':
                return $this->_manager->createUrl( $action.'/article/'.$model->getId() );
            default:
                return $this->_manager->createUrl( $action.'/'.$model->getId() );
                break;
        }


        /*
        switch( strtolower($action) ) {
            default:
                return $this->_manager->createUrl( $action , array('id'=>$model->getId()) );
                break;
        }
        */

    }

}
